package com.spzx.product.controller;



import com.github.pagehelper.PageHelper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.api.vo.ProductSkuVo;
import com.spzx.product.query.SkuQuery;
import com.spzx.product.service.ChannelService;
import com.spzx.product.service.ProductSkuService;
import com.spzx.product.vo.IndexDataVo;
import com.spzx.product.vo.ItemVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "聚合数据")
@RestController
@RequestMapping("/channel")
@Slf4j
@Log4j2
public class ChannelController extends BaseController {


    @Autowired
    private ProductSkuService productSkuService;

    @Autowired
    private ChannelService channelService;


    /**
     * 获取首页商品数据
     * @return
     */
    @Operation(summary = "获取首页商品数据")
    @GetMapping("/index")
    public AjaxResult index() {
       IndexDataVo indexDataVo = channelService.getIndexData();
        return success(indexDataVo);
    }

    /**
     * 获取商品分类嵌套列表
     * @return
     */
    @Operation(summary = "获取商品分类嵌套列表")
    @GetMapping("/category")
    public AjaxResult category() {
        return success(channelService.getCategoryVoList());
    }

    /**
     * 获取商品详情
     * @param pageNum
     * @param pageSize
     * @param skuQuery
     * @return
     */
    @Operation(summary = "显示商品列表")
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public TableDataInfo skuList(
            @Parameter(description = "当前页码")
            @PathVariable Integer pageNum,
            @Parameter(description = "每页显示条数")
            @PathVariable Integer pageSize, SkuQuery skuQuery) {

        PageHelper.startPage(pageNum, pageSize);
        List<ProductSkuVo> list = productSkuService.selectProductSkuList(skuQuery);

        return getDataTable(list);
    }

    /**
     * 获取全部品牌
     * @return
     */
    @Operation(summary = "获取全部品牌")
    @GetMapping("/brand")
    public AjaxResult selectBrandAll() {
        return success(channelService.getBrandAll());
    }

    /**
     * 获取当前商品详情页数据
     * @param skuId
     * @return
     */
    @Operation(summary = "商品详情")
    @GetMapping("/item/{skuId}")
    public AjaxResult item(
            @Parameter(description = "skuId")
            @PathVariable("skuId") Long skuId) {
        try {
            ItemVo itemVo = channelService.getItemVoBySkuId(skuId);

            return success(itemVo);
        } catch (Exception e) {
            log.error("商品详情查询异常:{}", e.getMessage());
            throw new ServiceException("商品详情查询异常");
        }
    }

}


